<?php

	class ShopCategoryController extends CommonController{

		public $model;

		public function __init(){
			parent:: __init();

			$this->model=K('Shop_category');
			
		}

		public function Index(){
			//调取数据

			$categoryData = $this->model->all();

			$categoryData = Data::tree($categoryData,'cname','cid','pid');

			$this->assign('categoryData',$categoryData);

			// p($categoryData);die;

			//导入首页
			$this->display();
		}


		public function add(){

			if (IS_POST) {

				if (!$this->model->add_category()) {

					$this->error($this->model->error);
				}

				$this->success('添加成功');
			}

			$this->display();
		}


		public function add_son(){
			//调用类型表中的数据
			$typeData = K('Shop_type')->all();

			$this->assign('typeData',$typeData);

			if (IS_POST) {

				if (!$this->model->addsonCategory()) {

					$this->error($this->model->error);
				}

				$this->success('添加子分类成功');
			}
			
			$this->display();
		}

		public function edit_son(){
			//调用类型表中的数据
			$typeData = K('Shop_type')->all();

			$this->assign('typeData',$typeData);

			//调入老数据
			$cid = Q('get.cid',0,'intval');

			$oldData = K('Shop_category')->where("cid={$cid}")->find();

			$this->assign('oldData',$oldData);

			if (IS_POST) {
				
				if (!$this->model->edit($cid)) {
					$this->error($this->model->error);
				}

				$this->success('修改成功');
			}
			$this->display();
		}

		public function del(){
			//获得删除的分类id
		 	$cid = Q('get.cid',0,'intval');
		 	//判断是否有子集
		 	if ($this->model->where("pid=$cid")->find()) {

		 		$this->error('请先删除子集分类');
		 	}

		 	$this->model->where("cid=$cid")->delete();

		 	$this->success('删除成功');

		}
	}







?>